package com.hanbitexam.action;

import java.util.ArrayList;
import java.util.List;

import com.hanbitexam.dao.QuestionDao;
import com.hanbitexam.dao.TestDao;
import com.hanbitexam.vo.QuestionVo;
import com.hanbitexam.vo.TestVo;

public class TestAction {
	
	List<TestVo> list;
	int idx;	//문제지의 인덱스
	List<QuestionVo> question_list;
	String q_idx,selected_idx;	//문제의 보기가 구분자로 들어간 값
	String testname,writer;
	
	
	public String getTestname() {
		return testname;
	}

	public void setTestname(String testname) {
		this.testname = testname;
	}

	public String getSelected_idx() {
		return selected_idx;
	}

	public void setSelected_idx(String selected_idx) {
		this.selected_idx = selected_idx;
	}

	public String getWriter() {
		return writer;
	}

	public void setWriter(String writer) {
		this.writer = writer;
	}

	public String getQ_idx() {
		return q_idx;
	}

	public void setQ_idx(String q_idx) {
		this.q_idx = q_idx;
	}

	public int getIdx() {
		return idx;
	}

	public void setIdx(int idx) {
		this.idx = idx;
	}

	public List<QuestionVo> getQuestion_list() {
		return question_list;
	}

	public void setQuestion_list(List<QuestionVo> question_list) {
		this.question_list = question_list;
	}

	public List<TestVo> getList() {
		return list;
	}

	public void setList(List<TestVo> list) {
		this.list = list;
	}

	public String load(){
		//TestDao dao = new TestDao();
		System.out.println("+++ TestAction's load() +++");
		System.out.println("testname : "+testname);
		try {
			
			if(testname!=null || !testname.trim().equals("")){
				list = TestDao.selectAllTest(testname);
		
			}else{
		
		
				list = TestDao.selectAllTest();
		}
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "success";
	}
	
	public String add(){
		
		try{
			TestVo vo = new TestVo();			
			vo.setQ_idx(q_idx);
			vo.setTestname(testname);
			vo.setWriter(writer);
			TestDao.insertTest(vo);
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return "success";
	}
	
	public String modify(){
		try{
			TestVo vo = new TestVo();
			vo.setIdx(idx);
			vo.setQ_idx(q_idx);
			vo.setTestname(testname);
			vo.setWriter(writer);
			TestDao.updateTest(vo);
						
		}catch(Exception e){
			e.printStackTrace();
		}
		return "success";
	}
	
	public String examload(){
		

		try {
			//q_idx = dao.selectAllTestExam(idx);
			System.out.println("q_idx : "+q_idx);
			getexamdata(q_idx);
			
		} catch (Exception e) {
			e.printStackTrace();
		}		
		return "success";
	}	
	
	
	public String del(){
		//TestDao dao = new TestDao();
		try{
			TestDao.deleteTest(idx);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "success";
	}
	
	public String examdel(){	//문제지의 문제를 삭제하는 액션
		
		System.out.println("q_idx : "+q_idx);
		System.out.println("시험지idx : "+selected_idx);
		getdelq_idx(q_idx, String.valueOf(selected_idx));
		TestVo vo = new TestVo();
		TestDao dao = new TestDao();
		vo.setQ_idx(q_idx);
		vo.setIdx(idx);
		try {
			dao.updateExamDel(vo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		return "success";
	}
		
	public String examadd(){	//문제지의 문제를 삭제하는 액션
		System.out.println("넣으려는 시험지의 인덱스 : "+idx);
		System.out.println("넣으려는 시험지의 q_idx : "+q_idx);
		System.out.println("추가하려는 문제의 인덱스 : "+selected_idx);
		
		q_idx=getaddq_idx(q_idx, selected_idx);
		if(q_idx.equals("fail")){
			return "fail";
		}
		TestVo vo = new TestVo();
		vo.setIdx(idx);
		vo.setQ_idx(q_idx);
		TestDao dao = new TestDao();
		try {
			dao.updateExamDel(vo);
		} catch (Exception e) {			
			e.printStackTrace();
		}
		
		return "success";
	}	
	
	public String loadtest(){
		
		return "success";
	}
	
	
	public void getexamdata(String str){	//구분자가 들어가있는 보기값을 배열로 만들어주는클래스
		q_idx = str;
		String[] aa;
		aa=str.split("!@");
		System.out.println("aa[0] : "+aa[0]);
		QuestionDao dao = new QuestionDao();
		question_list = new ArrayList<QuestionVo>();
	
		try{
			for(int i=0;i<aa.length;i++){
				QuestionVo vo=new QuestionVo();				
				vo=dao.selectQuestion(Integer.parseInt(aa[i]));
				System.out.println("aa[i] : "+aa[i]);
				
				question_list.add(vo);
				
			}
			System.out.println("question_list의 크기 : "+question_list.size());
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	
	public void getdelq_idx(String str,String str2){	//문제지에서 문제를 삭제하고 다시
			//구분자를 삽입해 쿼리를 날릴 q_idx값을 만들기 위한 클래스

		String[] aa;
		System.out.println("지울문제값 : "+str2);
		aa= str.split("!@");
		q_idx="";
		//String complete_idx="";		
		for(int i=0;i<aa.length;i++){
			if(!aa[i].equals(str2)){
				if(!q_idx.equals(""))
				q_idx+="!@";
				q_idx+=aa[i];
			}
			
		}		
		System.out.println("q_idx : "+q_idx);
	}
	public String getaddq_idx(String str,String str2){	//문제지에서 문제를 삭제하고 다시
		//구분자를 삽입해 쿼리를 날릴 q_idx값을 만들기 위한 클래스

	//System.out.println("더할문제값 : "+str2);	
	//String complete_idx="";
	String[] aa = str.split("!@");
	for(int i=0;i<aa.length;i++){
		if(aa[i].equals(str2)){
			q_idx = "fail";
			return q_idx;
		}
	}
		
	StringBuffer sb=new StringBuffer();
	sb.append(q_idx);
	
	if( !(q_idx.equals("") || q_idx==null)  ){
		sb.append("!@");
	}
		sb.append(str2);		
		q_idx=sb.toString();
		System.out.println("q_idx : "+q_idx);		
				
		return q_idx;
	}	
}
